/**
 * 
 */
package com.fyp.client.ptma.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fyp.client.ptma.AbstractActivity;
import com.fyp.client.ptma.R;
import com.fyp.client.ptma.model.Application;

/**
 * @author yahya
 * 
 */
public class CustomAdapterApplicationsList extends BaseAdapter
{

	private List<Application> apps = new ArrayList<Application>();
	private Context context;
	LayoutInflater mInflater;

	public CustomAdapterApplicationsList(Context context)
	{
		this.context = context;

		mInflater = LayoutInflater.from(context);

	}

	public void setData(List<Application> data)
	{

		if (data != null)
		{
			apps.clear();
			for (Application appEntry : data)
			{
				apps.add(appEntry);
			}
			notifyDataSetChanged();
		}

	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return apps.size();
	}

	@Override
	public Application getItem(int arg0)
	{
		// TODO Auto-generated method stub
		return apps.get(arg0);
	}

	@Override
	public long getItemId(int arg0)
	{

		return 0;
	}

	private class ViewHolder
	{
		TextView tvAppName;
		TextView tvComment;
		ImageView ivAppIcon;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup vg)
	{
		final ViewHolder holder;
		if (convertView == null)
		{

			convertView = mInflater.inflate(R.layout.application_row, null);

			holder = new ViewHolder();
			// holder.imgBg = (ImageView) convertView
			// .findViewById(R.id.img_fullModeBg);
			holder.ivAppIcon = (ImageView) convertView
					.findViewById(R.id.ivIcon);

			holder.tvAppName = (TextView) convertView
					.findViewById(R.id.tvAppName);

			holder.tvComment = (TextView) convertView
					.findViewById(R.id.tvComment);

			convertView.setTag(holder);
		} else
		{

			holder = (ViewHolder) convertView.getTag();
		}

		Application app = apps.get(position);
		if (app.getInstalledVersion() == null)
		{
			String version = AbstractActivity.getVersionByPackage(
					app.getLatestVersion(), context);
			if (version != null)
			{
				app.setInstalledVersion(version);

			}

		}
		if (app.getLatestVersion() == null)
		{
			int size = app.getApplicationVersions().size();
			if (size > 0)
			{
				String latestVer = app.getApplicationVersions().get(size - 1)
						.getVersionPk().getVersion();
				app.setLatestVersion(latestVer);
			}
		}

		if (app.getInstalledVersion() == null
				|| app.getInstalledVersion() != null
				&& !(app.getLatestVersion().equals(app.getInstalledVersion())))
		{
			holder.tvComment.setVisibility(View.VISIBLE);
			holder.tvComment.setText("New");
		} else
		{
			holder.tvComment.setVisibility(View.GONE);
		}

		holder.tvAppName.setText(app.getApplicationName());
		holder.ivAppIcon.setImageBitmap(app.getApplicationIcon());

		return convertView;
	}

}
